package com.hckjsec.website.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteLawstatute;
import com.hckjsec.website.service.IWebSiteLawstatuteService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 法律法规Controller
 * 
 * @author cyt
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/lawstatute")
public class WebSiteLawstatuteController extends BaseController
{
    @Autowired
    private IWebSiteLawstatuteService webSiteLawstatuteService;

    /**
     * 查询法律法规列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebSiteLawstatute webSiteLawstatute)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteLawstatute.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteLawstatute> list = webSiteLawstatuteService.selectWebSiteLawstatuteList(webSiteLawstatute);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出法律法规列表
     */
    @Log(title = "法律法规", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebSiteLawstatute webSiteLawstatute)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteLawstatute.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteLawstatute> list = webSiteLawstatuteService.selectWebSiteLawstatuteList(webSiteLawstatute);
        ExcelUtil<WebSiteLawstatute> util = new ExcelUtil<WebSiteLawstatute>(WebSiteLawstatute.class);
        return util.exportExcel(list, "lawstatute");
    }

    /**
     * 获取法律法规详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webSiteLawstatuteService.selectWebSiteLawstatuteById(id));
    }

    /**
     * 新增法律法规
     */
    @Log(title = "法律法规", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebSiteLawstatute webSiteLawstatute)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteLawstatute.getContent(),""))){//解密
            webSiteLawstatute.setContent(Base64Converter.decode(webSiteLawstatute.getContent()));
        }else{
            webSiteLawstatute.setContent(webSiteLawstatute.getContent());
        }
        webSiteLawstatute.setDeptId(SecurityUtils.getDeptId());
        webSiteLawstatute.setOperator(SecurityUtils.getUserId());
        return toAjax(webSiteLawstatuteService.insertWebSiteLawstatute(webSiteLawstatute));
    }

    /**
     * 修改法律法规
     */
    @Log(title = "法律法规", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebSiteLawstatute webSiteLawstatute)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteLawstatute.getContent(),""))){//解密
            webSiteLawstatute.setContent(Base64Converter.decode(webSiteLawstatute.getContent()));
        }else{
            webSiteLawstatute.setContent(webSiteLawstatute.getContent());
        }
        return toAjax(webSiteLawstatuteService.updateWebSiteLawstatute(webSiteLawstatute));
    }

    /**
     * 删除法律法规
     */
    @Log(title = "法律法规", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webSiteLawstatuteService.deleteWebSiteLawstatuteByIds(ids));
    }
}
